import complex_query from '@dfeidao/atom-nodejs/db/complex-query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { productid, systemid } from '../../atom/config';
import time from '../../atom/get_data';
interface Message {
	train_number: string;
}




// 表名	train_material
// 标题	培训资料
// 字段名称	字段标题	字段类型	字段长度	是否为空	是否主键
// _id	id	string	50	ⅹ	√
// productid	产品ID	string	50	√	ⅹ
// train_number	培训编号	string	64	√	ⅹ
// name_training_material	培训资料名称	string	64	√	ⅹ
// address_training_material	培训资料地址	document	64	√	ⅹ


export default async function atom(msg: Message, action_id: string, session_id: string) {
	log(`Service begin`);
	const query = await complex_query(action_id, session_id, systemid);
	const res1 = await query
		.add_field('train_plan', 'train_number', 'train_number')
		.add_field('train_plan', 'training_name', 'training_name')
		.add_field('train_plan', 'lecturer_number', 'lecturer_number')
		.add_field('train_plan', 'lecturer_name', 'lecturer_name')
		.add_field('train_plan', 'train_start_time', 'train_start_time')
		.add_field('train_plan', 'training_time', 'training_time')
		.add_field('train_plan', 'training_location', 'training_location')
		.add_field('train_plan', 'training_content', 'training_content')
		.add_field('train_plan', 'to_state', 'to_state')
		.add_field('train_plan', 'take_effect', 'take_effect')


		.where_eq('train_plan', 'train_number', msg.train_number)
		.inner_join('train_plan', 'training_objects', ['train_number', 'train_number']).add_field('training_objects', 'safety_department_no', 'safety_department_no')
		.add_field('training_objects', 'safety_department_name', 'safety_department_name').inner_join('train_plan', 'train_material', ['train_number', 'train_number']).add_field('train_material', 'name_training_material', 'name_training_material')
		.add_field('train_material', 'address_training_material', 'address_training_material').page(1, 1).exec();

	const res = res1.map((v) => {
		const c = time(productid, 'train_start_time', v);
		return time(productid, 'training_time', c);
	});
	log(`Service end`);
	return res;
}
